from django.db import models
from django.contrib.auth.models import User
from utils.base_model import BaseMixin
from commodity.models import Commodity


# Create your models here.
class Customer(BaseMixin):
    id = models.AutoField(primary_key=True)
    email = models.EmailField()
    password = models.CharField(max_length=160)  # 保存的密码是加密后的密码，长度给多点
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.email

    def customer_nickname(self):
        return self.customerdetail.nickname

    customer_nickname.short_description = '昵称'

    def customer_address(self):
        return self.customerdetail.address

    customer_nickname.short_description = '地址'

    def customer_birthday(self):
        return self.customerdetail.birthday

    customer_nickname.short_description = '生日'

    class Meta:
        db_table = 'customer'


class CustomerDetail(models.Model):
    id = models.AutoField(primary_key=True)
    nickname = models.CharField(max_length=40, unique=True, null=True)
    portrait = models.TextField(null=True)
    phone = models.CharField(max_length=12, unique=True, null=True)
    age = models.IntegerField(null=True)
    sex = models.IntegerField(null=True)
    address = models.TextField(null=True)
    birthday = models.DateField(null=True)
    customer = models.OneToOneField('Customer', on_delete=models.CASCADE)

    class Meta:
        db_table = 'detail'


class ShoppingCar(models.Model):
    money = models.FloatField()
    subject = models.TextField(verbose_name='商品描述')
    options = models.TextField(verbose_name='商品选项ID')   # 格式'6 7'
    number = models.IntegerField(verbose_name='购买数量', default=1)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE)
    user = models.ForeignKey(Customer, on_delete=models.CASCADE)

    def __str__(self):
        return f'{self.number}-{self.subject}'

    class Meta:
        db_table = 'shopping_car'
